/*
 * @Author: 高稀 562949576@qq.com
 * @Date: 2022-05-23 17:28:50
 * @LastEditors: 代琪 1529992814@qq.com
 * @LastEditTime: 2022-08-02 11:07:22
 * @FilePath: \code\ctp-static-portal-front\src\utils\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export function isElementInViewport(el) {
  //获取元素是否在可视区域
  var rect = el.getBoundingClientRect();
  return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
    rect.right <= (window.innerWidth || document.documentElement.clientWidth)
  );
}

export const methods = {
  /**
   * @description: 根据传入url打开窗口
   * @param {String} url 传入url
   * @return {*}
   */
  open(url) {
    window.open(url, '_blank');
  },
  /**
   * @description: 传入配置文件, 返回配置高度
   * @param {String} height
   * @return {Number}最终高度
   */
  getContentHeight({ sections, height }) {
    //减去20上下padding
    // 某些栏目不需要减padding
    const excludeSectionArr = ['bannerImageSection', 'richMediaSection'];
    return excludeSectionArr.includes(sections) ? +height || 300 : (+height || 300) - 20;
  },
  /**
   * @description: 获取一行展示多少条数据、每条多宽
   * @param {Number} containerWidth 当前行宽
   * @param {Number} columnWidth 列宽
   * @return {Object}{
   *                  count: Number
   *                  width: Number
   *                  }
   */
  getColumnData(containerWidth, columnWidth = 150) {
    //先算出一行多少列
    const count = Math.floor(containerWidth / columnWidth);
    //再算一列多少宽
    const width = Math.floor(containerWidth / count);
    return {
      count,
      width,
    };
  },

  /**
   * @description: 获取url中的参数
   * @param {String} key 要获取的url的key
   * @return {Number}
   */
  getUrlParam(key) {
    let u = arguments[1] || window.location.href,
      reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)'),
      r = u.substr(u.indexOf('?') + 1).match(reg);
    return r != null ? decodeURI(r[2]) : '';
  },
};

const { userAgent } = navigator;
export const data = {
  isIE8:
    ((userAgent.indexOf('MSIE') >= 0 || userAgent.indexOf('Trident') >= 0) &&
      /msie 8\.0/i.test(userAgent)) ||
    ((userAgent.indexOf('MSIE') >= 0 || userAgent.indexOf('Trident') >= 0) &&
      /msie 7\.0/i.test(userAgent)),
  isIE9: userAgent.indexOf('MSIE 9.0') >= 0 && userAgent.indexOf('Trident') >= 0,
};
